package com.itobase.statusDetect.Service.impl;

import com.itobase.statusDetect.Model.Organization;
import com.itobase.statusDetect.Model.Teacher;
import com.itobase.statusDetect.Model.Teaching;
import com.itobase.statusDetect.Repository.OrganizationRepository;
import com.itobase.statusDetect.Repository.TeacherRepository;
import com.itobase.statusDetect.Repository.TeachingRepository;
import com.itobase.statusDetect.Service.ClientService;
import com.itobase.statusDetect.Service.TeachingService;
import com.itobase.statusDetect.Util.AuthUtil;
import com.itobase.statusDetect.Util.Message;
import com.itobase.statusDetect.Util.StringUtils;
import com.itobase.statusDetect.VO.TeacherVO;
import com.itobase.statusDetect.VO.TeachingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author htyu
 * @date 2019/4/25
 */
@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    TeachingRepository teachingRepository;

    @Autowired
    TeachingService teachingService;

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    OrganizationRepository organizationRepository;



    public Map<String ,Object> getTeaching(String keyword){
        List<Teaching> list = teachingRepository.findAll();
        List<TeachingVO> list_vo = new ArrayList<>();
        List<TeachingVO> result;
        TeachingVO teachingVO;
        for(Teaching teaching: list){
            teachingVO = teachingService.teachingConvertVO(teaching);
            list_vo.add(teachingVO);
        }
        if(StringUtils.isNotEmpty(keyword)){
            result = list_vo.stream().filter(teachingVO1 ->
                    teachingVO1.getName().contains(keyword)||
                    teachingVO1.getAddress().toLowerCase().contains(keyword.toLowerCase())||
                    teachingVO1.getTeacher().contains(keyword)||
                    teachingVO1.getSchoolTime().contains(keyword))
                    .collect(Collectors.toList());
        }else {
            result = list_vo;
        }
        long number = teachingRepository.count();
        HashMap<String,Object> map = new HashMap<>();
        map.put("not_signed","");
        map.put("number",number);
        map.put("list",result);
        return map;
    }

    public TeacherVO teacherConvertVO(Teacher teacher){
        Organization organization = organizationRepository.findById(teacher.getOrganizationID()).get();
        TeacherVO vo = new TeacherVO();
        vo.setId(teacher.getId());
        vo.setName(teacher.getName());
        vo.setDepartment(organization.getName());
        return vo;
    }

    public List<TeacherVO> getTeacherList(String keyword) throws Throwable{
        String organizationID = AuthUtil.getOrganizationId();
        List<Teacher> list = teacherRepository.findAllByOrganizationID(organizationID);
        List<TeacherVO> list_vo = new ArrayList<>();
        List<TeacherVO> result;
        TeacherVO teacherVO;
        for(Teacher teacher : list){
            teacherVO = teacherConvertVO(teacher);
            list_vo.add(teacherVO);
        }
        if(StringUtils.isNotEmpty(keyword)){
            result = list_vo.stream().filter(teacherVO1 ->
                    teacherVO1.getName().contains(keyword) || teacherVO1.getDepartment().contains(keyword))
                    .collect(Collectors.toList());
        }else {
            result = list_vo;
        }
        return result;
    }



}
